package com.elitel.frame.base.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

//import com.elitel.common.redis.RedisDbIndex;
//import com.elitel.common.redis.service.RedisCacheService;
import com.elitel.frame.base.component.RedisCacheTreeConfig;
import com.elitel.frame.base.component.RedisCatchBasicServer;
import com.elitel.frame.base.service.CfgDbService;
import com.elitel.frame.base.service.CfgFieldService;
import com.elitel.frame.base.service.CfgTableService;
import com.elitel.frame.base.service.DBServerService;
import com.elitel.frame.base.service.TreeConfigFileService;
import com.elitel.frame.business.service.DataBaseMetadataService;
import com.elitel.frame.main.entity.BaseTreeconfig;
import com.elitel.frame.main.entity.CfgExttree;
import com.elitel.frame.main.entity.CfgField;
import com.elitel.frame.main.entity.CfgTable;
import com.elitel.frame.main.entity.SysPrjservice;
import com.elitel.frame.main.entity.ext.BaseTreeAef;
import com.elitel.frame.main.entity.ext.Census;
import com.elitel.frame.main.entity.ext.CfgExtTreeExt;
import com.elitel.frame.main.entity.ext.ExtBaseTreeconfig;
import com.elitel.frame.main.entity.vo.JsonResult;

@RequestMapping("/system/treeConfig")
@Controller
public class TreeConfigController {
	
	@Autowired
	private TreeConfigFileService treeConfigServiceImpl;
	
	@Resource
	private CfgTableService cfgTableServiceImpl;
	
	@Resource
	private CfgFieldService cfgFieldServiceImpl;
	
	@Resource
	private CfgDbService cfgDbService;
	
	@Resource
	private DataBaseMetadataService dataBaseMetadataServiceImpl;
	
//	@Resource
//	private RedisCacheService redisCacheService;
	
	@Resource
	private RedisCatchBasicServer redisCatchBasicServer;
	
	@Resource
	private RedisCacheTreeConfig redisCacheTreeConfig;
	
	@Resource
	private DBServerService dBServerServiceImpl;


	//刪除资源树
	@RequestMapping("/deleteTreeConfig")
	@ResponseBody
	public Object deleteTreeConfig(HttpServletRequest request){
		String guid = request.getParameter("keys");
		JsonResult jsonResult = new JsonResult();
		try{
			String message = treeConfigServiceImpl.deleteTreeConfig(guid);
			jsonResult.setMsg("删除成功");
			jsonResult.setSuccess(true);
		}catch (Exception e) {
			jsonResult.setMsg("删除失败");
			jsonResult.setSuccess(false);
		}
		return jsonResult;
	}
	
	//获取资源树列表
	@RequestMapping(value="/getTreeList")
	@ResponseBody
	public  List<Census> getTreeList(HttpServletRequest request){
		List<Census> list = treeConfigServiceImpl.getTreeList();
		return list;
	}
	//修改资源树列表
	@RequestMapping(value="/updateTreeList",method=RequestMethod.POST,consumes="application/json")
	@ResponseBody
	public String updateTreeList(@RequestBody CfgExttree cfgTreeExt,HttpServletRequest request){
		String result = "0";
		String message = treeConfigServiceImpl.updateTreeList(cfgTreeExt);
		if ("success".equals(message)) {
			redisCacheTreeConfig.setTreeing();
			result = "1";
//			redisCacheTreeConfig.selectTreeConfig(cfgTreeExt.getGuid());
		}
		return result;
	}
	
	
	//删除资源树列表
	@RequestMapping(value="/deleteTreeList")
	@ResponseBody
	public String deleteTreeList(HttpServletRequest request){
		String result = "0";
		String guid = request.getParameter("guid");
		String message = treeConfigServiceImpl.deleteTreeList(guid);
		if ("success".equals(message)) {
			redisCacheTreeConfig.setTreeing();
			result = "1";
		}
		return result;
	}
	//添加资源树
	@RequestMapping(value="/insertTreeList",method=RequestMethod.POST,consumes="application/json")
	@ResponseBody
	public String insertTreeList(@RequestBody CfgExtTreeExt cfgTreeExt,HttpServletRequest request){
		CfgExttree cfgExttree = new CfgExttree();
		cfgExttree.setCatchtime(cfgTreeExt.getCatchtime());
		cfgExttree.setDtDbKey(cfgTreeExt.getDtDbKey());
		cfgExttree.setGuid(cfgTreeExt.getGuid());
		cfgExttree.setIscatch(cfgTreeExt.getIscatch());
		cfgExttree.setIslazyload(cfgTreeExt.getIslazyload());
		cfgExttree.setIsusetop(cfgTreeExt.getIsusetop());
		cfgExttree.setServicename(cfgTreeExt.getServicename());
		cfgExttree.setTopnode(cfgTreeExt.getTopnode());
		String message = treeConfigServiceImpl.insertTreeList(cfgExttree);
		int a  =0;
		if ("success".equals(message)) {
			SysPrjservice sysPrjservice = new SysPrjservice();
			sysPrjservice.setGuid(String.valueOf(System.currentTimeMillis()));
			sysPrjservice.setPcode(cfgTreeExt.getHidProjectid());
			sysPrjservice.setServicetype(cfgTreeExt.getHidServertype());
			sysPrjservice.setServicecode(cfgTreeExt.getGuid());
			 a = dBServerServiceImpl.insertSystem(sysPrjservice);
		}
		if (a>0) {
			message = "success";
		}else {
			message = "fail";
		}
		if ("success".equals(message)) {
			redisCacheTreeConfig.setTreeing();
		}

		String result = "0";
		if(message.equals("success")){
			result = "1";
		}
		return result;
	}
	
	@RequestMapping("/selectOneTree")
	@ResponseBody
	public BaseTreeconfig selectOneTree(HttpServletRequest request){
		String guid = request.getParameter("dbid");
		BaseTreeconfig baseTreeconfig = treeConfigServiceImpl.selectOneTree(guid);
		return baseTreeconfig;
	}
	
	//添加节点
	@RequestMapping(value="/insertTreeNode",method=RequestMethod.POST,consumes="application/json")
	@ResponseBody
	public String inserTreeNode(@RequestBody BaseTreeconfig baseTree,HttpServletRequest request){
		String message = treeConfigServiceImpl.insertTreeNode(baseTree);
		String key  = baseTree.getServiceid();
		redisCacheTreeConfig.setTreeConfig(key);
//		redisCacheTreeConfig.selectTreeConfig(baseTree.getServiceid());
		String result = "0";
		if(message.equals("success")){
			result = "1";
		}
		return result;
	}
	
	
	
	//删除节点
	@RequestMapping(value="/deleteTreeNode")
	@ResponseBody
	public String deleteTreeNode(HttpServletRequest request){
		String treeid = request.getParameter("treeid");
		String seid = request.getParameter("seid");
		String guid = request.getParameter("idss");
		BaseTreeconfig baseTree = treeConfigServiceImpl.selectOneTree(guid);
		String message = treeConfigServiceImpl.deleteTreeNode(guid,treeid,seid);
		String key  = baseTree.getServiceid();
		redisCacheTreeConfig.setTreeConfig(key);
		String result = "0";
		if(message.equals("success")){
			result = "1";
		}
		return result;
	}
	//修改树节点
	@RequestMapping(value="/updateTreeNode",method=RequestMethod.POST,consumes="application/json")
	@ResponseBody
	public String updateTreeNode(@RequestBody BaseTreeconfig baseTreeconfig,HttpServletRequest request){
		String result = "0";
		String message = treeConfigServiceImpl.updateTreeNode(baseTreeconfig);
		String key  = baseTreeconfig.getServiceid();
		if ("success".equals(message)) {
			redisCacheTreeConfig.setTreeConfig(key);
			result = "1";
//			redisCacheTreeConfig.selectTreeConfig(baseTreeconfig.getServiceid());
		}
		return result;
	}
	//查看树节点
	@RequestMapping(value="/selectTreeNode")
	@ResponseBody
	public List<BaseTreeconfig> selectTreeNode(HttpServletRequest request){
		String treeid = request.getParameter("treeId");
		String sguid = request.getParameter("sguid");
		List<BaseTreeconfig> list = treeConfigServiceImpl.selectTreeNode(treeid,sguid);
		return list;
	}
	
	//获取数据库下面所有的表
	@RequestMapping(value="/selectAllTable")
	@ResponseBody
	public Map<String, Object> selectAllTable(HttpServletRequest request){
		String dbid = request.getParameter("dbid");
		List<CfgTable> list = cfgTableServiceImpl.selectTableMap(dbid);
		Map<String, Object> map = new HashMap<>();
		for(CfgTable c:list){
			if (c.getDtTabletype()==1) {
				map.put( c.getDtConfigId(),c.getDtNameCn());
			}
		}
		return map;
	}
	//根据表名获取表里的字段
	@RequestMapping(value="/selectTableColumn")
	@ResponseBody
	public Map<String, Object> selectTableColumn(HttpServletRequest request){
		Map<String, Object> map = null;
		String dbid = request.getParameter("dbid");
		String dbkey = request.getParameter("dbkey");
		String tableName = cfgTableServiceImpl.selectTableName(dbid);
		map = treeConfigServiceImpl.selectColumnMap(dbid);
		return map;
	}
	
	//查看树节点详情
	@RequestMapping("/selectTreeFile")
	@ResponseBody
	public CfgExttree  selectTreeFile(HttpServletRequest request){
		String guid = request.getParameter("sysId");
		CfgExttree cfgTreeExt = treeConfigServiceImpl.selectTreeFile(guid);
		return cfgTreeExt;
	}
	//查看添加的id是否已经存在
	@RequestMapping("/selectCountGUID")
	@ResponseBody
	public int selectCountGUID(HttpServletRequest request){
		String guid = request.getParameter("dbKey");
		int count = treeConfigServiceImpl.selectCountGUID(guid);
		return count;
	}
	
	//查看详情
	@RequestMapping(value="/selectTreeshow",method=RequestMethod.POST,consumes="application/json")
	@ResponseBody
	public List<ExtBaseTreeconfig>  selectTreeshow(@RequestBody BaseTreeAef baseTreeAef,HttpServletRequest request){
		String guid = baseTreeAef.getSysId();
		String treeid = baseTreeAef.getKeys();
		List<ExtBaseTreeconfig> list = treeConfigServiceImpl.selectTreeShow(guid,treeid);
		for(ExtBaseTreeconfig e:list){
//			if (e.getFieldKey()!=null) {
//				CfgField cfgField = redisCacheTreeConfig.cfgFieldRedisCache(e.getDtConfigId(),e.getFieldKey());
//				e.setFieldKey(cfgField.getDfNameCn());
//			}
//			if (e.getFieldPKey()!=null) {
//				CfgField cfgField2 = redisCacheTreeConfig.cfgFieldRedisCache(e.getDtConfigId(),e.getFieldPKey());
//				e.setFieldPKey(cfgField2.getDfNameCn());
//			}
//			if (e.getFieldShow()!=null) {
//				CfgField cfgField3 = redisCacheTreeConfig.cfgFieldRedisCache(e.getDtConfigId(),e.getFieldShow());
//				e.setFieldShow(cfgField3.getDfNameCn());
//			}
			if (e.getFieldKey()!=null) {
			CfgField cfgField = treeConfigServiceImpl.getAllListCfgField(e.getDtConfigId(),e.getFieldKey());
			e.setFieldKey(cfgField.getDfNameCn());
			}
			if (e.getFieldPKey()!=null&&!"".equals(e.getFieldPKey())) {
				CfgField cfgField2 =treeConfigServiceImpl.getAllListCfgField(e.getDtConfigId(),e.getFieldPKey());
				e.setFieldPKey(cfgField2.getDfNameCn());
			}
			if (e.getFieldShow()!=null) {
				CfgField cfgField3 =treeConfigServiceImpl.getAllListCfgField(e.getDtConfigId(),e.getFieldShow());
				e.setFieldShow(cfgField3.getDfNameCn());
			}
			String xx = treeConfigServiceImpl.selectTableCN(e.getDtConfigId());
			if (xx!=null) {
				e.setDtConfigId(xx);
			}
		}
		return list;
	}
	
	
	
}
